const app = getApp();
const {
  myInfo,
  notice,
  teamlist,
  changeTeam,
  homedata
} = require('../../config.js')

Page({
  data: {
    main:{
      newtask:0,
      intask: 0,
      oktask: 0,
      nosign: 0,
      signing: 0,
      qingsuan: 0
    },
    show: false,
    actions: [],
    notice: app.globalData.notice,
    noticeTips: app.globalData.noticeTips
  },
  onLoad: function () {
    console.log(333,'in_index')
    app.globalData.orderDetailFlag = true
  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    wx.showNavigationBarLoading();
    // 获取公司邀请信息
    app.appRequest({
      url: myInfo,
      success: (res) => {
        if (res.data.haveNews > 0) {
          wx.showTabBarRedDot({
            index: 1,
          })
        } else {
          wx.hideTabBarRedDot({
            index: 1,
          })
        }
        // 获取公司信息
        this.setData({
          data: res.data
        })
        // 获取管理员Type
        app.globalData.maintType = res.data.type
        console.log('maintType', app.globalData.maintType)
      }
    })

    // 获取数据条数
    app.appRequest({
      url: homedata,
      method: 'post',
      success: (res) => {
        wx.hideNavigationBarLoading();
        if (res.code == 0) {
          this.setData({
            main:{
              newtask: res.data.newOrder,
              intask: res.data.dealOrder,
              oktask: res.data.overOrder,
              nosign: res.data.toSignPrint,
              signing: res.data.signPrint,
              qingsuan: res.data.clearPrint
            }
          })
          // console.log(this.data.main)
        }else{
          wx.showToast({
            title: res.msg
          })
        }
      }
    })
    // 获取最新公告
    app.appRequest({
      url: notice,
      success: (res) => {
        if (res.data.bean) {
          this.setData({
            notice: res.data.bean.title
          })
          app.globalData.notice = res.data.bean.title;
          app.globalData.newnoticeId = res.data.bean.id;

          if (res.data.bean.id == app.globalData.noticeId) {
            this.setData({
              noticeTips: 1
            })
            app.globalData.noticeTips = 1;
          } else {
            this.setData({
              noticeTips: 0
            })
            app.globalData.noticeTips = 0;
          }
        } else {
          this.setData({
            noticeTips: 1
          })
          app.globalData.noticeTips = 1;
        }
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();

    // 获取数据条数
    app.appRequest({
      url: homedata,
      method: 'post',
      success: (res) => {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        if (res.code == 0) {
          this.setData({
            main: {
              newtask: res.data.newOrder,
              intask: res.data.dealOrder,
              oktask: res.data.overOrder,
              nosign: res.data.toSignPrint,
              signing: res.data.signPrint,
              qingsuan: res.data.clearPrint
            }
          })
        } else {
          wx.showToast({
            title: res.msg
          })
        }
      }
    })

  },
  navigatorToNotice_list() {
    wx.navigateTo({
      url: '/pages/notice_list/notice_list',
    })
  },
  // 关闭公告
  closeNotice: function () {
    this.setData({
      noticeTips: 1
    })
    app.globalData.noticeTips = 1;
    app.globalData.noticeId = app.globalData.newnoticeId;
  },
  navitoNewTask(){
    wx.navigateTo({
      url: '/pages/workOrder/workOrder?type=2',
    })
  },
  navitoTasking(){
    wx.navigateTo({
      url: '/pages/workOrder/workOrder?type=3',
    })
  },
  navitoTasked(){
    wx.navigateTo({
      url: '/pages/workOrder/workOrder?type=4',
    })
  },
  navitodevice1(){
    wx.navigateTo({
      url: '/pages/device/device?type=2',
    })
  },
  navitodevice2(){
    wx.navigateTo({
      url: '/pages/device/device?type=3',
    })
  },
  navitodevice3(){
    wx.navigateTo({
      url: '/pages/device/device?type=4',
    })
  },
  navitoGetData(){
    wx.showToast({
      title: "功能正在搭建，敬请期待",
      icon:'none'
    })
    // wx.navigateTo({
    //   url: '/pages/gatherData/getData1/getData1',
    // })
  },
  changeteam(){
    wx.navigateTo({
      url: '/pages/myTeam/myTeam',
    })
    // this.setData({ show: true });
    // app.appRequest({
    //   url: teamlist,
    //   success: (res) => {
    //     let data = res.data.list;
    //     let data1 = [];
    //     data.forEach((item) => {
    //       if (item.id == res.data.current){
    //         data1.push({ name: item.name, id: item.id, disabled: true})
    //       }else{
    //         data1.push({ name: item.name, id: item.id})
    //       }
    //     })
    //     this.setData({
    //       actions: data1
    //     })
    //   }
    // })
  },
  onClose() {
    this.setData({ show: false });
  },
  onSelect(event) {
    console.log(event.detail);
    let that = this;
    wx.showModal({
      title: '切换团队',
      content: '请您确认是否要切换团队',
      success(res) {
        if (res.confirm) {
          app.appRequest({
            url: changeTeam,
            method: 'POST',
            data: { merchantId: event.detail.id },
            success: (res) => {
              that.setData({ show: false });
              // 刷新
              app.appRequest({
                url: myInfo,
                success: (res) => {
                  let data = res.data;
                  that.setData({
                    data
                  })
                }
              })
              // 获取数据条数
              app.appRequest({
                url: homedata,
                method: 'post',
                success: (res) => {
                  wx.hideNavigationBarLoading();
                  wx.stopPullDownRefresh();
                  if (res.code == 0) {
                    that.setData({
                      main: {
                        newtask: res.data.newOrder,
                        intask: res.data.dealOrder,
                        oktask: res.data.overOrder,
                        nosign: res.data.toSignPrint,
                        signing: res.data.signPrint,
                        qingsuan: res.data.clearPrint
                      }
                    })
                  } else {
                    wx.showToast({
                      title: res.msg
                    })
                  }
                }
              }) 
            }
          })
        }
      }
    })
    
  }
})